﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PingPongMain
{
    public static class PaddleCollision
    {
        private static void GameOver(Ball ball)
        {
            if (Score.Lives == 0)
            {
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2);
                Console.WriteLine("Game over. Your score is : " + Score.PlayerScore);
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2 + 1);
                Console.WriteLine("Press Y for a new game and N to exit!");
                ConsoleKeyInfo key = Console.ReadKey();
                while (key.Key != ConsoleKey.Y && key.Key != ConsoleKey.N)
                {
                    key = Console.ReadKey();
                }
                if (key.Key == ConsoleKey.Y)
                {
                    ball.SetBallAtTheMiddle();
                    ball.BallDirectionRight = true;
                    ball.BallDirectionUp = true;
                    Score.PlayerScore = 0;
                    Score.Lives = 3;
                }
                if (key.Key == ConsoleKey.N)
                {
                    System.Environment.Exit(0);
                }
            }
            else
            {
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2);
                Console.WriteLine("You lose a Life! You have {0} lives left!", Score.Lives);
                ball.SetBallAtTheMiddle();
                ball.BallDirectionRight = true;
                ball.BallDirectionUp = true;
            }
        }

        public static void BallCollision(Ball ball, Paddle firstPaddle, Paddle secondPaddle)
        {
            if (ball.GameObjCoords.Col == 0)
            {
                Score.Lives--;
                GameOver(ball);
                Console.ReadKey();
            }
            if (ball.GameObjCoords.Col == Console.WindowWidth - 1)
            {
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2);
                Console.WriteLine("You win 100 points!");
                ball.SetBallAtTheMiddle();
                ball.BallDirectionRight = false;
                ball.BallDirectionUp = true;
                Score.PlayerScore += 100;
                Console.ReadKey();
            }

            if (ball.GameObjCoords.Col < 3)
            {
                if (ball.GameObjCoords.Row >= firstPaddle.GameObjCoords.Row
                    && ball.GameObjCoords.Row < firstPaddle.GameObjCoords.Row + firstPaddle.Length)
                {
                    ball.BallDirectionRight = true;
                    Score.PlayerScore += 10;
                }
            }
            if (ball.GameObjCoords.Col > Console.WindowWidth - 3)
            {
                if (ball.GameObjCoords.Row >= secondPaddle.GameObjCoords.Row
                     && ball.GameObjCoords.Row < secondPaddle.GameObjCoords.Row + secondPaddle.Length)
                {
                    ball.BallDirectionRight = false;
                }
            }
        }
        public static Bonus BonusCollision(Bonus bonus, Paddle paddle)
        {
            if (bonus.GameObjCoords.Row >= paddle.GameObjCoords.Row && bonus.GameObjCoords.Row <= paddle.GameObjCoords.Row + paddle.Length && bonus.GameObjCoords.Col == 1)
            {
                Score.PlayerScore += 20;
                bonus.GameObjCoords.Col = PrepareField.WorldCols - 3;
                bonus.GameObjCoords.Row = TomCatPingPongMain.rand.Next(1, PrepareField.WorldRows - 2);
            }
            else if(bonus.GameObjCoords.Col == 1)
            {
                bonus.GameObjCoords.Col = PrepareField.WorldCols - 3;
                bonus.GameObjCoords.Row = TomCatPingPongMain.rand.Next(1, PrepareField.WorldRows - 2);
            }
            return bonus;

        }
    }
}
